Title: SQL W3S (By-BKK) / 017 Count
Tags: Obsidian_to_Anki

Q: What does the SQL COUNT() function do?
A: It returns the number of rows that match a specified criterion

Q: True/False: COUNT() can be used to count rows based on a condition
A: True

Q: What is the result of SELECT COUNT(*) FROM Products?
A: It gives the total number of rows in the Products table

Q: Does COUNT(column_name) count NULL values?
A: No, it ignores NULL values

Q: Reverse card:
It ignores NULL values
A: What does COUNT(column_name) do with NULL values?

Q: How do you count products where ProductName is not null?
A: Use SELECT COUNT(ProductName) FROM Products

Q: What does adding a WHERE clause to COUNT() let you do?
A: It lets you count rows that meet a condition

Q: How do you count products with Price greater than 20?
A: SELECT COUNT(ProductID) FROM Products WHERE Price > 20

Q: True/False: COUNT(*) counts all rows, including those with NULLs
A: True

Q: How can you ignore duplicate values when counting?
A: Use COUNT(DISTINCT column_name)

Q: Reverse card:
Use COUNT(DISTINCT column_name)
A: How do you count unique values in a column?

Q: What does the SQL statement SELECT COUNT(DISTINCT Price) FROM Products do?
A: It counts the number of different prices in the Products table

Q: How do you give a name to a COUNT column in the result?
A: Use AS to create an alias (e.g. SELECT COUNT(*) AS [Number of records])

Q: True/False: You can use COUNT() with GROUP BY to get totals for categories
A: True

Q: How does COUNT() work with GROUP BY?
A: It returns the number of records for each group/category

Q: Write a query to count products in each CategoryID
A: SELECT COUNT(*), CategoryID FROM Products GROUP BY CategoryID

Q: Reverse card:
SELECT COUNT(*) AS [Number of records] FROM Products
A: What query names the COUNT result column as 'Number of records'?

Q: What is the syntax for using COUNT() on a column with a condition?
A: SELECT COUNT(column_name) FROM table_name WHERE condition

Q: Reverse card:
SELECT COUNT(*) FROM Products;
A: Which SQL query finds the total number of rows in the Products table?